package com.lanou.renrenxin.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.chat.FileMessageBody;
import com.easemob.chat.ImageMessageBody;
import com.easemob.chat.LocationMessageBody;
import com.easemob.chat.TextMessageBody;
import com.easemob.exceptions.EaseMobException;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.adapter.PinyinAdapter;
import com.lanou.renrenxin.base.BaseActivity;
import com.lanou.renrenxin.custommodule.AssortView;
import com.lanou.renrenxin.dao.ChatList;
import com.lanou.renrenxin.tools.FormatTime;
import com.lanou.renrenxin.tools.StringValues;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zouhui on 16/3/18.
 */
public class ForwardAMsgActivity extends BaseActivity implements ExpandableListView.OnChildClickListener, PinyinAdapter.OnChildItemClickListener {
    private PinyinAdapter adapter;
    private ExpandableListView eListView;
    private AssortView assortView;
    private List<String> names;
    private Handler handler;
    private EMMessage emMessage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forward_msg);
        Bundle bundle = this.getIntent().getExtras();
        emMessage = bundle.getParcelable(StringValues.DIALOG_EMMESSAGE_FORWARD);
        eListView = (ExpandableListView) findViewById(R.id.expandListView_activity_forward_msg_elist);
        assortView = (AssortView) findViewById(R.id.assort_activity_forward_msg);
        eListView.setOnChildClickListener(this);
        names = new ArrayList<String>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    List<String> usernames = EMContactManager.getInstance().getContactUserNames();//需异步执行
                    Message message = new Message();
                    message.obj = usernames;
                    message.what = 667;
                    handler.sendMessage(message);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }


            }
        }).start();
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 667) {
                    names = (List<String>) msg.obj;
                    adapter = new PinyinAdapter(ForwardAMsgActivity.this, names);
                    adapter.setOnChildItemClickListener(ForwardAMsgActivity.this);
                    eListView.setAdapter(adapter);


                    //???????
                    for (int i = 0, length = adapter.getGroupCount(); i < length; i++) {
                        eListView.expandGroup(i);
                    }
                }
                return false;
            }
        });


        assortView.setOnTouchAssortListener(new AssortView.OnTouchAssortListener() {
            View layoutView = LayoutInflater.from(ForwardAMsgActivity.this)
                    .inflate(R.layout.alert_dialog_menu_layout, null);
            TextView text = (TextView) layoutView.findViewById(R.id.content);
            PopupWindow popupWindow;

            @Override
            public void onTouchAssortListener(String s) {
                int index = adapter.getAssort().getHashList().indexOfKey(s);
                if (index != -1) {
                    eListView.setSelectedGroup(index);
                    ;
                }
                if (popupWindow != null) {
                    text.setText(s);
                } else {
                    popupWindow = new PopupWindow(layoutView, 200, 200, false);
                    popupWindow.showAtLocation(getWindow().getDecorView(),
                            Gravity.CENTER, 0, 0);
                }
                text.setText(s);
            }

            @Override
            public void onTouchAssortUP() {
                if (popupWindow != null)
                    popupWindow.dismiss();
                popupWindow = null;
            }
        });
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        return false;
    }

    @Override
    public void onItemClick(String username) {
        EMConversation conversation = EMChatManager.getInstance().getConversation(username);
        ChatList chat = new ChatList();
        chat.setUsername(username);
        chat.setMUsername(EMChatManager.getInstance().getCurrentUser());
        chat.setBody(judgeTypeBody(emMessage.getType()));
        chat.setTime(FormatTime.formatTime(StringValues.NO_TIME));
        emMessage.setReceipt(username);
        conversation.addMessage(emMessage);

        EMChatManager.getInstance().sendMessage(emMessage, new EMCallBack() {
            @Override
            public void onSuccess() {

                finish();
            }

            @Override
            public void onError(int i, String s) {
                finish();
            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
        Toast.makeText(ForwardAMsgActivity.this, "转发成功", Toast.LENGTH_SHORT).show();
    }

    public String judgeTypeBody(EMMessage.Type type) {
        switch (type) {
            case TXT:
                return ((TextMessageBody) emMessage.getBody()).getMessage();

            case LOCATION:
                return ((LocationMessageBody) emMessage.getBody()).getAddress();

            case VOICE:
                return "[语音]";

            case VIDEO:
                return "[视频]";

            case IMAGE:
                return "[图片]" + ((ImageMessageBody) emMessage.getBody()).getFileName();

            case FILE:
                return "[文件]" + ((FileMessageBody) emMessage.getBody()).getFileName();

            default:
                return "";

        }
    }
}
